<?php
/**
 * @Notes:【】
 * @Date: 2022-06-26 0:23
 */

namespace Yj\Providers;

use App\Tool\Collect;
use Hyperf\Utils\Arr;
use Yj\Core\ServiceProvider;

class CollectProvider extends ServiceProvider
{
    public function boot()
    {
        Collect::macro('set', function ($field, $callback) {
            $value = $this->get($field);
            if ($callback instanceof \Closure) {
                $callback = $callback($value);
            }
            $this->offsetSet($field, $callback);
        });

        Collect::macro('check', function ($fields, $value = null, $callback = true) {

            /** @var Collect $self */
            $self = $this;

            if (is_callable($value)) {
                $callback = $value;
                $value = null;
            }
            if (!is_array($fields)) {
                $fields = [$fields];
            }

            $value = _Collect(is_null($value) ? [$value] : Arr::wrap($value));
            $fields = _Collect($fields);
            $fields->transform(function ($field) use ($self) {
                $val = $self->get($field);
                return $val;
            });
            if ($fields->filter(function ($val) use ($value, $self) {
                    return false === collect($value)->containsStrict($val);
                })->count() == count($fields)) {
                return $callback(...$fields->toArray());
            }

            return false;


//                $field = value($field);

//
//            if ($this->offsetExists($field)) {

//                $val = $this->getParams()->get($field);
//                if (false === collect($value)->containsStrict($val)) {
//                    return (is_callable($callback) ? $callback(...$val) : $callback);
//                }
//            }
//            return false;
        });
        // TODO: Implement boot() method.
    }

}